<script setup>
import { reactive, ref } from 'vue';

import { User, Lock, Key } from '@element-plus/icons-vue'

import { ElMessage } from 'element-plus'

import { useRouter } from 'vue-router'

import api from '@/api'
const admin = ref({
  phone: '',
  password: '',
  captcha: '',
});

// 获取路由对象
const router = useRouter();

// 验证码图片数据
const captcha = ref("");

// 加载验证码
loadCaptcha();

async function onSubmit() {
  const result = await api.login(admin.value);
  // const index=result.indexOf("+");
  // const key=result.substring(0,index);
  // const data=result.substring(index+1)
  // sessionStorage.setItem("captchaKey",key)
  // captcha.value=result;
  if (result) {
    sessionStorage.setItem("token", result);
    setTimeout(function () {
      router.push('/');
    }, 1800)
  }
}

// 加载验证码
async function loadCaptcha() {
  const result = await api.getCaptcha();
  const index = result.indexOf("+");
  const key = result.substring(0, index);
  const data = result.substring(index + 1)
  sessionStorage.setItem("captchaKey", key)
  captcha.value = data;
}
</script>


<template>
  <div class="login-bg">
    <el-form class="form-login" :model="admin">
      <el-row>
        <el-col :span="10">
          <img style="width: 50px" src="@/static/logo.png" alt="万达影院"/>
        </el-col>
        <el-col :span="14">
          <h1>系统登录</h1>
        </el-col>
      </el-row>
      <el-form-item>
        <el-input v-model="admin.phone"
                  placeholder="请输入手机号"
                  :prefix-icon="User"
                  maxlength="16"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="admin.password"
                  placeholder="请输入密码"
                  show-password
                  :prefix-icon="Lock"
                  maxlength="16"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="admin.captcha"
                  placeholder="验证码"
                  :prefix-icon="Key"
                  maxlength="5"
                  class="input-verCode"></el-input>
        <img :src="captcha"
             class="img-verCode" @click="loadCaptcha" style="width:100px">
      </el-form-item>
      <el-form-item>
        <el-button class="btn-login" type="danger" @click="onSubmit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>

.login-bg {
  height: 100%;
  background-image: url('../static/back.jpg');
  background-repeat: no-repeat;
  background-position: center;
  background-attachment: fixed;
  background-size: cover;
}

.form-login {
  width: 280px;
  padding: 20px;
  position: absolute;
  top: 28%;
  left: calc(65% - 175px);
  background-color: #f2f2f2;
  box-shadow: 10px 10px 30px #000;
}

.btn-login {
  width: 100%;
}

h1 {
  text-align: center;
  font-size: 24px;
  margin-bottom: 18px;
  height: 40px;
  line-height: 40px;
}

.input-verCode {
  width: 160px;
}

.img-verCode {
  width: 80px;
  height: 30px;
}
</style>